package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.pojo.Category;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.SetmealDish;
import com.itheima.pojo.SetmealDto;
import com.itheima.service.CategoryService;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;
    @Autowired

    private RedisTemplate redisTemplate;

@PostMapping
    public R addSetmeal(@RequestBody SetmealDto setmealDto){
    Set keys = redisTemplate.keys("setmeal_*");
    redisTemplate.delete(keys);
    setmealService.save(setmealDto);
    List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
    String setmealName = setmealDto.getName();
    LambdaQueryWrapper<Setmeal> wrapper=new LambdaQueryWrapper<>();
    wrapper.eq(Setmeal::getName,setmealName);
    Setmeal setmeal = setmealService.getOne(wrapper);
    Long setmealId = setmeal.getId();

    for (SetmealDish setmealDish : setmealDishes) {
        setmealDish.setSetmealId(setmealId);
        setmealDishService.save(setmealDish);
    }


    return R.success("添加成功！");
    }
    @GetMapping("/page")
    public R page(Integer page,Integer pageSize,String name){
        Page pageBean=new Page(page,pageSize);
        LambdaQueryWrapper<Setmeal> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(name!=null,Setmeal::getName,name);
        setmealService.page(pageBean,wrapper);
        long total = pageBean.getTotal();

        Page page1=new Page();
        page1.setTotal(total);
        List<Setmeal> records = pageBean.getRecords();
        List<SetmealDto> list=new ArrayList<>();

        for (Setmeal setmeal : records) {
            SetmealDto setmealDto=new SetmealDto();
            BeanUtils.copyProperties(setmeal,setmealDto);
            Long categoryId = setmeal.getCategoryId();
            Category category = categoryService.getById(categoryId);
            setmealDto.setCategoryName(category.getName());
            LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper=new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
            List<SetmealDish> setmealDishList = setmealDishService.list(lambdaQueryWrapper);
            setmealDto.setSetmealDishes(setmealDishList);
            list.add(setmealDto);
        }
        page1.setRecords(list);


        return R.success(page1);

    }
    @DeleteMapping
    public R deleteSetmeal(@RequestParam("ids") List<Long> ids){
        Set keys = redisTemplate.keys("setmeal_*");
        redisTemplate.delete(keys);


        long count = ids.stream().count();

        int num=0;


        for (Long id : ids) {
            Setmeal setmeal = setmealService.getById(id);
            if (setmeal.getStatus()==0){
                setmealService.removeById(id);
            }else {
                num++;
            }
        }

        if (count==num){
            return R.error("所选的套餐是启售状态，不能删除");
        }

//        setmealService.removeByIds(ids);

        for (Long id:ids){
            LambdaQueryWrapper<SetmealDish> wrapper=new LambdaQueryWrapper<>();
            wrapper.eq(SetmealDish::getSetmealId,id);

    setmealDishService.remove(wrapper);
        }


    return R.success("删除成功！");

    }

    @PostMapping("/status/{status}")
    public R updateStatus(@PathVariable Integer status,@RequestParam("ids") List<Long> ids){
        Set keys = redisTemplate.keys("setmeal_*");
        redisTemplate.delete(keys);
    for (Long id : ids) {
        Setmeal setmeal = setmealService.getById(id);
        setmeal.setStatus(status);
        setmealService.updateById(setmeal);
    }
    return R.success("操作成功！");


    }
    @GetMapping("/{id}")
    public R getSetmeal(@PathVariable Long id){
        SetmealDto setmealDto=new SetmealDto();
        Setmeal setmeal = setmealService.getById(id);
        Long categoryId = setmeal.getCategoryId();
        Category category = categoryService.getById(categoryId);
        String categoryName = category.getName();
        BeanUtils.copyProperties(setmeal,setmealDto);
        setmealDto.setCategoryName(categoryName);
        LambdaQueryWrapper<SetmealDish> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(wrapper);
        setmealDto.setSetmealDishes(list);
        return R.success(setmealDto);

    }

    @PutMapping
    @Transactional
    public R update(@RequestBody SetmealDto setmealDto){
        Set keys = redisTemplate.keys("setmeal_*");
        redisTemplate.delete(keys);

        Setmeal setmeal=new Setmeal();
    BeanUtils.copyProperties(setmealDto,setmeal);
    setmealService.updateById(setmeal);
    LambdaQueryWrapper<SetmealDish> wrapper=new LambdaQueryWrapper<>();
    wrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
    setmealDishService.remove(wrapper);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());
            setmealDishService.save(setmealDish);

        }

        return R.success("操作成功！");

    }


@GetMapping("/list")
public R list(Long categoryId,Integer status){
    String setmealKey="setmeal"+categoryId+"_"+status;
    List<Setmeal> list = (List<Setmeal>) redisTemplate.opsForValue().get(setmealKey);
    if (list!=null){
        R.success(list);
    }



    LambdaQueryWrapper<Setmeal> wrapper=new LambdaQueryWrapper<>();
    wrapper.eq(Setmeal::getCategoryId,categoryId);
    wrapper.eq(Setmeal::getStatus,status);
    list = setmealService.list(wrapper);
    redisTemplate.opsForValue().set(setmealKey,list);

    return R.success(list);

}

}
